<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2019/1/23
 * Time: 13:54
 */
//多文件上传的接收
$month = date('Ym', time());
//设置上传位置，获取上传文件的目录


define('BASE_PATH', str_replace('\\', '/', realpath(dirname(__FILE__) . '/')) . "/");
$dir = BASE_PATH . "upload/" . $month . "/";
//上传成功反回的数据
$arr = array(
    'code' => 0,
    'msg' => '上传成功',
    'data' => array(
        'src' => $dir . $_FILES["file"]["name"]
    ),
);
$file_info = $_FILES['file'];
$file_error = $file_info['error'];
//判断目录是否存在，不存在则创建目录
if (!is_dir($dir)) {
    mkdir($dir, 0777, true);
};
$file = $dir . $_FILES['file']['name'];
if (!file_exists($file)) {
    if ($file_error == 0) {
        $arr['code'] ="200";
        if (move_uploaded_file($_FILES['file']['tmp_name'], $dir . $_FILES['file']['name'])) {
            $arr['msg'] = "上传成功";
        } else {
            $arr['msg'] = "上传失败";
        }
    } else {
        $arr['code'] ="2";
        switch ($file_error) {
            case 1:
                $arr['msg'] = '文件超过规定大小';
                break;
            case 2:
                $arr['msg'] = '超过了表单max_file_size限制的大小';
                break;
            case 3:
                $arr['msg'] = '文件部分被上传';
                break;
            case 4:
                $arr['msg'] = '没有选择上传文件';
                break;
            case 6:
                $arr['msg'] = '没有找到临时文件';
                break;
            case 7:
            case 8:
                $arr['msg'] = '系统错误';
                break;
        }


    }
}
else{
    $arr['code'] ="1";
    $arr['msg'] = "文件已存在";
}

echo json_encode($arr);